<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="方案名称"
            prop="title"
            :rules="[{ required: true, message: '请输入方案名称', trigger: 'blur' }]"
            style="width: 100%"
            maxlength="10"
            show-word-limit
          >
            <el-input
              v-model="form.title"
              placeholder="请输入方案名称"
              maxlength="10"
              show-word-limit
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="主题切换规则"
            prop="dateType"
            :rules="[{ required: true, message: '请选择主题切换规则', trigger: 'change' }]"
            style="width: 100%"
            maxlength="10"
            show-word-limit
          >
            <el-radio-group v-model="form.dateType">
              <el-radio
                v-for="item in types"
                :key="'type-'+item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="状态"
            prop="zt"
            :rules="[{ required: true, message: '请选择状态', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.zt">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div class="custom-card">
            主题<div class="custom-add"><el-button type="text" icon="el-icon-plus" @click="doAdd">新增</el-button></div>
          </div>
        </el-col>
        <el-col :span="24" style="margin-top: 10px">
          <el-row v-for="(item,index) in form.theme" :key="'row-'+index" :gutter="20">
            <el-col :span="8">
              <el-form-item
                label="请选择主题"
                :prop="'theme.'+index+'.id'"
                :rules="[{ required: true, message: '请选择主题', trigger: 'change' }]"
                style="width: 100%"
              >
                <el-select v-model="item.id" clearable placeholder="请选择主题">
                  <el-option
                    v-for="item in themes"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="form.dateType === 1" :span="8">
              <el-form-item
                label="结束时间"
                :prop="'theme.'+index+'.jssj'"
                :rules="[{ required: true, message: '请选择主题', trigger: 'change' }]"
                style="width: 100%"
              >
                <el-time-select
                  v-model="item.jssj"
                  placeholder="结束时间"
                  :picker-options="{
                    start: '06:00',
                    step: '00:15',
                    end: '24:00'
                  }"
                />
              </el-form-item>
            </el-col>
            <el-col v-if="form.dateType === 2" :span="8">
              <el-form-item
                label="结束时间"
                :prop="'theme.'+index+'.jssj'"
                :rules="[{ required: true, message: '结束时间', trigger: 'change' }]"
                style="width: 100%"
              >
                <el-select v-model="item.jssj" clearable placeholder="结束时间">
                  <el-option
                    v-for="item in 30"
                    :key="item+'-d'"
                    :label="item + '号'"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="form.dateType === 3" :span="8">
              <el-form-item
                label="结束时间"
                :prop="'theme.'+index+'.kssj'"
                :rules="[{ required: true, message: '请选择主题', trigger: 'change' }]"
                style="width: 100%"
              >
                <el-date-picker
                  v-model="item.kssj"
                  placeholder="结束时间"
                  type="date"
                  format="MM月dd日"
                  value-format="MMdd"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8" style="text-align: right">
              <el-button type="text" icon="el-icon-delete" @click="toDelete(item)">删除</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { queryAllTheme } from '@/api/system/theme/index'
import { saveEntity, updateEntity } from '@/api/system/theme/plan'
export default {
  name: 'PlanEntity',
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      themes: [],
      types: [
        { id: 1, value: 1, label: '日周期切换' },
        { id: 2, value: 2, label: '月周期切换' },
        { id: 3, value: 3, label: '年周期切换' }
      ],
      status: [{ id: 0, value: false, label: '禁用' },
        { id: 1, value: true, label: '启用' }],
      form: {
        id: null,
        type: 2,
        zt: true,
        kssj: '',
        jssj: '',
        title: '',
        dateType: 1,
        theme: [
          {
            id: null,
            kssj: '',
            jssj: '',
            title: '',
            backGroundColor: '',
            fontColor: ''
          }
        ]
      }
    }
  },
  created() {
    this.initAllTheme()
  },
  methods: {
    init(data) {
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = data
      }
      this.dialogFormVisible = true
    },
    initAllTheme() {
      queryAllTheme().then(res => {
        const { data } = res
        this.themes = data.data
      }).catch(() => {
        this.themes = []
      })
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateEntity(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveEntity(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    doAdd() {
      const data = { id: null, kssj: '', jssj: '', title: '', backGroundColor: '', fontColor: '' }
      this.form.theme.push(data)
    },
    toDelete(item) {
      const index = this.form.theme.indexOf(item)
      if (index !== -1) {
        this.form.theme.splice(index, 1)
      }
    }
  }
}
</script>

<style scoped>
.custom-card {
  width: 100%;
  height: 60px;
  padding: 10px;
  box-sizing: border-box;
  border-radius: 2px;
  border-left: 5px solid #50bfff;
  background-color: #ecf8ff;
  line-height: 40px;
  position: relative;
}
.custom-add {
  position: absolute;
  width: 100px;
  height: 60px;
  right: 5px;
  top: 0px;
  text-align: center;
  line-height: 60px;
}
</style>
